/*
 * Visualization of gradient information.
 *
 */

#include <vtkImageCast.h>
#include <vtkImageConstantPad.h>
#include <vtkImageEuclideanToPolar.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageGaussianSmooth.h>
#include <vtkImageGradient.h>
#include <vtkImageHSVToRGB.h>
#include <vtkImageMagnify.h>
#include <vtkImageViewer.h>
#include <vtkMetaImageReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <iomanip>
#include <iostream>
#include <string>

#include <vtkJPEGReader.h>
#include <vtkImageViewer2.h>

#include <vtkImageActor.h>
#include <vtkImageMapper3D.h>
#include <vtkPolyDataMapper.h>
#include <vtkPlaneSource.h>
#include <vtkImageReader2Factory.h>


#include "DownloadManager.h"
#include "QtCore"
#include "QTime"


int main(int argc, char* argv[])
{
    QCoreApplication app(argc, argv);

//    DownloadManager manager;
////    QTimer::singleShot(0, &manager, SLOT(execute()));
//    QUrl url = QUrl::fromEncoded("https://t7.baidu.com/it/u=2621658848,3952322712&fm=193&f=GIF");
//    manager.doDownload(url);


    // Create the RenderWindowInteractor.
    vtkNew<vtkRenderWindowInteractor> iren;
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    iren->SetRenderWindow(renderWindow);


//    vtkNew<vtkJPEGReader> JPEGReader;
//    JPEGReader->SetFileName("/Users/dai/Work/vtk/VTK-Example/AMy/ImageMap/0.jpg");

    char *inputFilename = "/Users/dai/Work/vtk/VTK-Example/AMy/ImageMap/map2.jpg";

    // Read file
    vtkNew<vtkImageReader2Factory> readerFactory;
    vtkSmartPointer<vtkImageReader2> imageReader;
    imageReader.TakeReference(readerFactory->CreateImageReader2(inputFilename));
    imageReader->SetFileName(inputFilename);
    imageReader->Update();


//    // Convert the data to polar coordinates.
//    // The image magnitude is mapped into saturation value,
//    //  whilst the gradient direction is mapped into hue value.
//    vtkNew<vtkImageEuclideanToPolar> polar;
//    polar->SetInputConnection(JPEGReader->GetOutputPort());
////    polar->SetThetaMaximum(255.0);

    // Create an actor
    vtkNew<vtkImageActor> actor;
    actor->GetMapper()->SetInputConnection(imageReader->GetOutputPort());

    actor->Update();
    double *imageBounds = actor->GetBounds();
    std::cout << imageBounds[0] << "-" << imageBounds[1] << "-" << imageBounds[2] << "-" << imageBounds[3];
    double scale = 0.5;
    double x = -(imageBounds[1] - imageBounds[0]) * 0.5 * scale;
    double y = -(imageBounds[3] - imageBounds[2]) * 0.5 * scale;
    actor->SetPosition(x, y, 0);
    actor->SetScale(0.5);
    renderer->AddActor(actor);

    vtkNew<vtkPlaneSource> plane;
    plane->SetXResolution(10);
    plane->SetYResolution(10);
    plane->SetCenter(0, 0, -1);
    plane->SetOrigin(-500, 500, -1);
    plane->SetPoint1(500, 500, -1);
    plane->SetPoint2(-500, -500, -1);
    vtkNew<vtkPolyDataMapper> planeMapper;
    planeMapper->SetInputConnection(plane->GetOutputPort());
    vtkNew<vtkActor> planeActor;
    planeActor->SetMapper(planeMapper);
    renderer->AddActor(planeActor);

    renderWindow->Render();

    vtkSmartPointer<vtkActorCollection> actorCollection = renderer->GetActors();
    for (int i = 0; i < actorCollection->GetNumberOfItems(); ++i) {
        vtkSmartPointer<vtkObject> obj = renderer->GetActors()->GetItemAsObject(i);
        bool isa = obj->IsA("vtkObject");
//        if (obj->IsA("vtkObject")) {
//            vtkSmartPointer<vtkActor> actor = vtkActor::SafeDownCast(obj);
//            std::cout << "actor: " << actor->GetProperty()->GetColor()[0];
//        }
    }

    iren->Initialize();
    iren->Start();



    return EXIT_SUCCESS;
}